package com.qfedu.service.impl;

import com.qfedu.entity.ResultVo;
import com.qfedu.entity.User;
import com.qfedu.mapper.UserMapper;
import com.qfedu.service.UserService;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 作者：刘随阳
 * 时间: 2021/9/20 9:51
 * 描述：神光照耀,太白成瑞。六丙来迎,百福悠归
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Override
    public ResultVo login(String username, String password) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userName",username);
        List<User> users = userMapper.selectByExample(example);
        if (users == null || users.size() == 0){
            return ResultVo.fail("用户不存在");
        }
        User user = users.get(0);
        if(user.getUserPassword().equals(password)){
            JwtBuilder builder = Jwts.builder();
            String token = builder.setSubject(username)                                 //主题信息 token的数据
                    .setIssuedAt(new Date())                                            //生成时间
                    .setId(users.get(0).getUserId().toString())                         //将user的id设置为token的id
                    //.setClaims()                                                        //设置权限相关的信息
                    //使用redis不再设置token过期时间
                    //.setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))  //设置过期时间
                    .signWith(SignatureAlgorithm.HS256, "123456")                    //设置密码
                    .compact();
            stringRedisTemplate.opsForValue().set(token,username,30, TimeUnit.MINUTES);     //将登陆信息存入redis
            return ResultVo.success("登录成功",token);
        }
        return ResultVo.fail("账号或者密码有误");
    }

    @Override
    public ResultVo register(String username, String password,String phone, String code) {
        String s = stringRedisTemplate.boundValueOps("sms:" + phone).get();
        if (StringUtils.isEmpty(s)){
            return ResultVo.fail("验证码已过期") ;
        }
        if(!s.equals(code)){
            return ResultVo.fail("验证码错误");
        }
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userName",username);
        List<User> users = userMapper.selectByExample(example);
        if( users.size() != 0){
            return ResultVo.fail("用户名已存在");
        }
        User user1 = new User();
        user1.setUserName(username);
        user1.setUserPassword(password);
        user1.setUserPhone(phone);
        int insert = userMapper.insert(user1);
        if (insert == 1){
            stringRedisTemplate.delete("sms:"+phone);
            return ResultVo.success("注册成功！");
        }
        return ResultVo.fail("注册失败");
    }
}
